package com.qianfeng.wuyang.himalaya.activity;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RadioGroup;

import com.qianfeng.wuyang.himalaya.R;
import com.qianfeng.wuyang.himalaya.adapter.Fragment_PagerAdapter;
import com.qianfeng.wuyang.himalaya.fragment.AnchorFragment;
import com.qianfeng.wuyang.himalaya.fragment.BroadcastFragment;
import com.qianfeng.wuyang.himalaya.fragment.ClassifyFragment;
import com.qianfeng.wuyang.himalaya.fragment.MonthlyFragment;
import com.qianfeng.wuyang.himalaya.fragment.RbDownLoadFragment;
import com.qianfeng.wuyang.himalaya.fragment.RbListenerFragment;
import com.qianfeng.wuyang.himalaya.fragment.RbMineFragment;
import com.qianfeng.wuyang.himalaya.fragment.RecommendFragment;

import java.util.ArrayList;
import java.util.List;

import static com.qianfeng.wuyang.himalaya.R.id.base_fragment;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {


    private List<Fragment> fragmentList;
    private ViewPager viewpager_main;
    private TabLayout tablayout_main;
    private RadioGroup rg_main;

    //订阅听得fragment

    private  List<Fragment> list;
    private FragmentManager manager;
    private FragmentTransaction transaction;
    public DrawerLayout drawerLayout;

    //推荐里的fragment
    public FrameLayout drawercontainer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initDrawer();
        initView();
        initToolbar();
        initFragment();
    }

    private void initDrawer() {
        //对抽屉进行控制
        final FrameLayout  frameLayout = (FrameLayout) findViewById(R.id.drawer_container);
        drawerLayout= (DrawerLayout) findViewById(R.id.drawerLayout);
        drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
        ActionBarDrawerToggle taggle=new ActionBarDrawerToggle(this,drawerLayout,null,R.string.app_name,R.string.app_name){
            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
            }
            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
                if (frameLayout.getChildCount()>0) {
                    frameLayout.removeViewAt(0);
                }
                drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
            }
        };
        taggle.syncState();
        drawerLayout.addDrawerListener(taggle);
    }

    //初始化view
    private void initView(){
        viewpager_main = ((ViewPager) findViewById(R.id.viewpager_main));
        tablayout_main = ((TabLayout) findViewById(R.id.tablayout_main));
        rg_main = ((RadioGroup) findViewById(R.id.rg_main));



        //对抽屉进行控制
        final FrameLayout  frameLayout = (FrameLayout) findViewById(R.id.drawer_container);
        drawerLayout= (DrawerLayout) findViewById(R.id.drawerLayout);
        drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
        ActionBarDrawerToggle taggle=new ActionBarDrawerToggle(this,drawerLayout,null,R.string.app_name,R.string.app_name){
            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
            }
            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
                if (frameLayout.getChildCount()>0) {
                frameLayout.removeViewAt(0);
                }
                drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
            }
        };
        taggle.syncState();
        drawerLayout.addDrawerListener(taggle);



        //radioGroup的监听
        rg_main.setOnCheckedChangeListener(this);

    }
    //初始化toolbar
    private void initToolbar(){
        Toolbar toolbar_main = (Toolbar) findViewById(R.id.toolbar_main);
        setSupportActionBar(toolbar_main);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayShowTitleEnabled(false);



    }

    //初始化fragment
    private void initFragment() {
        // raidButton的跳转fragment的初始化
        list=new ArrayList<Fragment>();
        RbListenerFragment   blankFragment=new RbListenerFragment();
        RbDownLoadFragment  downLoadFragment =new RbDownLoadFragment();
        RbMineFragment mineFragment=new RbMineFragment();
        list.add(blankFragment);
        list.add(downLoadFragment);
        list.add(mineFragment);
        manager=getSupportFragmentManager();

         transaction=manager.beginTransaction();

         transaction.add(base_fragment,list.get(0));
        transaction.add(base_fragment,list.get(1));
        transaction.add(base_fragment,list.get(2));

//         Log.d("1620", "initFragment: "+list.size());

          transaction.hide(list.get(0));
          transaction.hide(list.get(1));
          transaction.hide(list.get(2));
          transaction.commit();

     //推荐类中的fragment的初始化
        fragmentList = new ArrayList<>();

        AnchorFragment anchorFragment = new AnchorFragment();
        BroadcastFragment broadcastFragment = new BroadcastFragment();
        MonthlyFragment monthlyFragment = new MonthlyFragment();
        RecommendFragment recommendFragment = new RecommendFragment();
        ClassifyFragment classifyFragment = new ClassifyFragment();

        fragmentList.add(recommendFragment);
        fragmentList.add(classifyFragment);
        fragmentList.add(broadcastFragment);
        fragmentList.add(monthlyFragment);
        fragmentList.add(anchorFragment);


        Fragment_PagerAdapter fragmentPagerAdapter = new Fragment_PagerAdapter(getSupportFragmentManager(),fragmentList);
        viewpager_main.setAdapter(fragmentPagerAdapter);
        tablayout_main.setupWithViewPager(viewpager_main);
     }

    //radiogroup的监听
    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        transaction=manager.beginTransaction();

        switch (i) {
            case R.id.rb_find:
                //点击刷新发现界面
                   transaction.hide(list.get(0));
                    transaction.hide(list.get(1));
                    transaction.hide(list.get(2));

              break;
            case R.id.rb_listener:
                //点击跳转到订阅界面
                showFragment(list,0);





                break;
            case R.id.rb_download:
                //点击跳转下载听界面
                showFragment(list,1);

                break;
            case R.id.rb_mine:
                //点击跳转我的界面
                showFragment(list,2);

                break;
        }



         transaction.commit();
     }
    //展示radioButton的fragment类
    public void showFragment(List<Fragment> list,int i){
        for (int i1 = 0; i1 < list.size(); i1++) {
            if(i1==i){
                transaction.show(list.get(i1));
            }else{
                transaction.hide(list.get(i1));
            }

        }
    }

    //跳转到分类fragment
    public void getClassfiyFragment(){
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
        ClassifyFragment classifyFragment = new ClassifyFragment();
//        viewpager_main.setCurrentItem();

    }
}
